package com.Xsoft.ShoppingList;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MyPickerActivity extends Activity {
	private static final int CLEAR_SELECTED_ITEMS_MENU_ITEM_ID = 1;
	private static final int ADD_SELECTED_ITEMS_TO_LIST_MENU_ITEM_ID = 2;
	private static final int CLEAR_ALL_ITEMS_MENU_ITEM_ID = 3;
	private static final int CLEAR_SELECTED_ITEMS_DAILOG_ID = 0;
	private static final int CLEAR_ALL_ITEMS_DAILOG_ID = 1;
	private ArrayList<Product> productList;
	private Resources resources;
	private ListView listView;
	private ProductPickerArrayAdapter aa;
	private ShoppingListBDAdapter bdAdapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.product_picker);
		productList = new ArrayList<Product>();
		resources = getResources();
		resources.getString(R.string.hello);
		listView = (ListView) findViewById(R.id.listViewProductPicker);
		int resID = R.layout.product_list_picker_item;
		aa = new ProductPickerArrayAdapter(this, resID, productList);
		listView.setAdapter(aa);
		bdAdapter = new ShoppingListBDAdapter(getApplicationContext());
		bdAdapter.open();
		ArrayList<Product> tmpProductList = bdAdapter.getAllroducts();
		Iterator<Product> tmpIt = tmpProductList.iterator();
		while (tmpIt.hasNext()) {
			Product tmpProd = tmpIt.next();
			if (!tmpProd.isInList()) {
				aa.add(tmpProd);
			}
		}
		aa.notifyDataSetChanged();
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		bdAdapter.updateProductList(productList);
		bdAdapter.close();
		super.onPause();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem bdClearAll = menu.add(0, CLEAR_ALL_ITEMS_MENU_ITEM_ID,
				Menu.NONE, R.string.db_clear_all_items);
		MenuItem bdClearSelected = menu.add(0,
				CLEAR_SELECTED_ITEMS_MENU_ITEM_ID, Menu.NONE,
				R.string.db_clear_selected_menu_items);
		MenuItem bdAddSelectedToList = menu.add(0,
				ADD_SELECTED_ITEMS_TO_LIST_MENU_ITEM_ID, Menu.NONE,
				R.string.db_add_selected_menu_items_to_list);
		return super.onCreateOptionsMenu(menu);
	}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
	// TODO Auto-generated method stub
	return super.onPrepareOptionsMenu(menu);
}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case CLEAR_ALL_ITEMS_MENU_ITEM_ID:
			//if(!aa.isEmpty())
			showDialog(CLEAR_ALL_ITEMS_DAILOG_ID);
			break;
		case CLEAR_SELECTED_ITEMS_MENU_ITEM_ID:
			showDialog(CLEAR_SELECTED_ITEMS_DAILOG_ID);
			break;
		case ADD_SELECTED_ITEMS_TO_LIST_MENU_ITEM_ID:
			finish();
			break;
		default:
			Log.w("Task MyPickerActivity", "No such case" + item.getItemId()
					+ "handler avaliable");
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder ad = new AlertDialog.Builder(this);
		ad.setTitle(R.string.alert_dialog_warning_title);
		switch (id) {
		case CLEAR_SELECTED_ITEMS_DAILOG_ID:
			ad.setMessage(R.string.alert_dailog_bd_clear_selected_items_message);
			ad.setPositiveButton(R.string.alert_dailog_yes_button_text,
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							Iterator<Product> it = productList.iterator();
							while (it.hasNext()) {
								Product tmpProduct = it.next();
								if (tmpProduct.isInList()) {
									it.remove();
									aa.notifyDataSetChanged();
									bdAdapter.removeProduct(tmpProduct);
								}
							}
						}
					});
			ad.setNegativeButton(R.string.alert_dailog_no_button_text,
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

						}
					});
			break;
		case CLEAR_ALL_ITEMS_DAILOG_ID:
			ad.setMessage(R.string.alert_dailog_bd_clear_all_items_message);
			ad.setPositiveButton(R.string.alert_dailog_yes_button_text,
					new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							bdAdapter.removeAllProducts();
							aa.clear();
							aa.notifyDataSetChanged();
						}
					});
			ad.setNegativeButton(R.string.alert_dailog_no_button_text,
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

						}
					});
			break;

		}
		ad.show();
		return super.onCreateDialog(id);
	}
}
